package com.jk11a.shangdaast.user;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.UserDao;
import com.jk11a.shangdaast.notice.Notice;

import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class RegistActivity extends Activity{

	private EditText userid;
	private EditText password;
	private EditText name;
	private Spinner college;
	private Spinner class_;
	private Adapter collegeAdapter;
	private Adapter classAdapter;
	private Button submitButton;
	
	User user=new User();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		userid=(EditText)findViewById(R.id.regist_writeuserid_edit);
		password=(EditText)findViewById(R.id.regist_writepsd_edit);
		name=(EditText)findViewById(R.id.regist_writename_edit);
		college=(Spinner)findViewById(R.id.regist_college_spin);
		class_=(Spinner)findViewById(R.id.regist_class_spin);
		submitButton=(Button)findViewById(R.id.regist_submit_btn);
		collegeAdapter=ArrayAdapter.createFromResource(this, R.array.college, android.R.layout.simple_spinner_item);
		classAdapter=ArrayAdapter.createFromResource(this, R.array.class_, android.R.layout.simple_spinner_item);
		college.setAdapter((SpinnerAdapter) collegeAdapter);
		class_.setAdapter((SpinnerAdapter) classAdapter);
		college.setVisibility(View.VISIBLE);
		class_.setVisibility(View.VISIBLE);
		
		submitButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				user.setName(name.getText().toString());
				user.setPassword(password.getText().toString());
				user.setClass_(class_.getSelectedItem().toString());
				user.setCollege(college.getSelectedItem().toString());
				user.setUserid(userid.getText().toString());
				new RegistThread(RegistActivity.this).start();	
			}
		});
		
	}
	
	public void regist_back(View v) { 
		this.finish();
	}

	private class RegistThread extends Thread {
		private RegistActivity regist2;

		public RegistThread(RegistActivity regist1) {
			regist2 = regist1;
		}
		
		@Override
		public void run() {
			UserDao userDao=new UserDao();
			User user2=new User();
			user2=userDao.regist(user);
			if(null==user2.getFlag()){
				regist2.handler.sendEmptyMessage(2);
				Intent intent = new Intent();
				intent.setClass(RegistActivity.this, RegistActivity.class);
				startActivity(intent);
				finish();	
			}
			
			if(user2.getFlag().equals("reg_hasuser")){
				regist2.handler.sendEmptyMessage(0);
				Intent intent = new Intent();
				intent.setClass(RegistActivity.this, RegistActivity.class);
				startActivity(intent);
				finish();	
			}else if(user2.getFlag().equals("reg_success")){
				regist2.handler.sendEmptyMessage(1);
				Intent intent = new Intent();
				intent.setClass(RegistActivity.this, Notice.class);
				startActivity(intent);
				finish();			
			}else{
				regist2.handler.sendEmptyMessage(2);
				Intent intent = new Intent();
				intent.setClass(RegistActivity.this, RegistActivity.class);
				startActivity(intent);
				finish();	
			}
			
			
		}

	}
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(RegistActivity.this, "用户名已存在", Toast.LENGTH_LONG)
				.show();
			case 1:
				Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_LONG)
						.show();
			case 2:
				Toast.makeText(RegistActivity.this, "注册失败", Toast.LENGTH_LONG).show();
				break;

			default:
				break;
			}
		}

	};
	
}
